package org.maggioni.springmvcsandbox.airport.model;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.util.Assert;

public class Flight {
	
	private List<FlightLeg> flightLegs;
	private BigDecimal totalCost;
	
	public Flight(List<FlightLeg> flightLegs, BigDecimal totalCost) {
		Assert.notNull(flightLegs);
		Assert.isTrue(flightLegs.size()>0, "Flight must have at least one leg");
		this.flightLegs = flightLegs;
		this.totalCost = totalCost;
	}

	public BigDecimal getTotalCost() {
		return totalCost;
	}
	
	public boolean isNonStop() {
		return (flightLegs.size()==1);
	}
	
	public FlightLeg getFirstLeg() {
		return flightLegs.get(0);
	}
	
	public Airport getDepartFrom() {
		return getFirstLeg().getDepartFrom();
	}
	
	public FlightLeg getLastLeg() {
		return flightLegs.get(flightLegs.size()-1);
	}
	
	public Airport getArriveAt() {
		return getLastLeg().getArriveAt();
	}
	
	public int getNumberOfLegs() {
		return flightLegs.size();
	}

	public long getTotalFlightTime() {
		Date start = getFirstLeg().getDepartOn();
		Date end = getLastLeg().getArriveOn();
		Assert.isTrue(end.compareTo(start)>0, "Start date must be before end date");
		return end.getTime()-start.getTime();
	}
	
}
